package collection.Demo14;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Author Miku
 * @Create 2021/4/25 12:58
 * Collections工具类
 */
public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(20);
        list.add(5);
        list.add(12);
        list.add(30);
        list.add(6);
        //sort排序
        System.out.println("排序前:"+list);
        Collections.sort(list);
        System.out.println("排序后:"+list);

        //binarySearch二分查找
        int i = Collections.binarySearch(list, 12);
        System.out.println(i);

        //copy复制
        ArrayList<Integer> dest = new ArrayList<>();
        for (int j = 0; j<list.size();j++) {
            dest.add(0);
        }
        Collections.copy(dest, list);
        System.out.println("复制后:"+dest);

        //reverse反转
        Collections.reverse(list);
        System.out.println("反转后"+list);

        //shuffle打乱
        Collections.shuffle(list);
        System.out.println("打乱后"+list);

        //补充: list转成数组
        Integer[] array = list.toArray(new Integer[0]);
        System.out.println(array.length);
        System.out.println(Arrays.toString(array));

        //补充: 数组转成集合
        String[] names = {"张三","李四","王五"};
        //数组转为集合时集合是一个受限集合，不能添加或删除元素
        List<String> nameList = Arrays.asList(names);
        System.out.println(nameList);

        //把基本类型数组转成集合时，需要修改为包装类型
        Integer[] nums = {100,200,300,400,500};
        List<Integer> ints = Arrays.asList(nums);
        System.out.println(ints);

    }
}
